import 'package:chathouse/constant/app_constant.dart';
import 'package:chathouse/model/member_info.dart';
import 'package:chathouse/pages/expert/buyWallet/recharge_bank_card.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/log.dart';
import 'package:chathouse/util/string_util.dart';
import 'package:chathouse/widgets/base_appbar.dart';
import 'package:flutter/material.dart';
// import 'package:package_info/package_info.dart';

import '../../../model/response_bean.dart';
import '../../../util/base_utils.dart';
import '../../../widgets/dialog/confirm_dialog.dart';
import '../../../widgets/my_input_widget.dart';

/// @class : ContactUsPage
/// @date : 2023-03-04 15:21
/// @name : LFC
/// @description : 联系我们
class ContactUsPage extends StatefulWidget {
  int type; //1 用戶 2專家
  ContactUsPage(this.type, {Key? key}) : super(key: key);

  @override
  State<ContactUsPage> createState() => _ContactUsPageState();
}

class _ContactUsPageState extends State<ContactUsPage> {
  TextEditingController companyController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController themeController = TextEditingController();
  TextEditingController contentController = TextEditingController();
  // PackageInfo? _packageInfo;

  @override
  void initState() {
    super.initState();
    // _initPackageInfo();
  }

  // Future<void> _initPackageInfo() async {
  //   final info = await PackageInfo.fromPlatform();
  //   setState(() {
  //     _packageInfo = info;
  //     LogUtil.d("_packageInfo ${_packageInfo.toString()}");
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: getAppBar(
          title: "聯絡我們",
          titleTextStyle:
              getTextStyle(30.sp, color: Colors.black, fontType: FontType.MID)),
      body: ListView(
        children: [
          Row(
            children: [
              ImageUtils.image(Res.email_reset_pwd, width: 33.w, height: 24.w),
              SizedBox(
                width: 10.w,
              ),
              Text(
                "電郵",
                style: getTextStyle(18.sp,
                    color: Colors.black, fontType: FontType.MID),
              ),
              SizedBox(
                width: 20.w,
              ),
              Text(
                "cs@chathouse.com.hk",
                style: getTextStyle(18.sp,
                    color: Colors.black, fontType: FontType.MID),
              ),
            ],
          ),
          SizedBox(
            height: 10.w,
          ),
          Container(
            color: Colors.black,
            height: 0.5.w,
          ),
          MyInputWidget(
            title: "聯絡人/公司名稱",
            textStyle: getTextStyle(18.sp, fontType: FontType.MID),
            controller: companyController,
          ).setContainer(margin: EdgeInsets.only(top: 12.w)),
          MyInputWidget(
            title: "你的電郵",
            textStyle: getTextStyle(18.sp, fontType: FontType.MID),
            controller: emailController,
          ).setContainer(margin: EdgeInsets.only(top: 12.w)),
          MyInputWidget(
            title: "主題",
            textStyle: getTextStyle(18.sp, fontType: FontType.MID),
            controller: themeController,
          ).setContainer(margin: EdgeInsets.only(top: 12.w)),
          Text(
            "内容",
            style: getTextStyle(18.sp, fontType: FontType.MID),
          ).setContainer(
            margin: EdgeInsets.only(top: 20.w),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child: TextField(
              controller: contentController,
              maxLines: 10,
              decoration: InputDecoration(
                hintStyle: getTextStyle(16.sp,
                    color: MyColors.gary_155155155, fontType: FontType.MID),
                border: InputBorder.none,
              ),
            ),
            height: 146.w,
            decoration: BoxDecoration(
                border: Border.all(color: MyColors.gary_155155155, width: 1.w),
                borderRadius: BorderRadius.circular(12.w)),
          ),
          SizedBox(height: 60.w),
          Row(
            //必须加Row 不加 会铺满宽度  无法指定宽度
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text("提交", style: getTextStyle(20.sp, color: Colors.white))
                  .setContainer(
                height: 50.w,
                width: 150.w,
                decoration: BoxDecoration(
                    color: widget.type == 1
                        ? MyColors.green_69157608
                        : MyColors.green_69157154,
                    borderRadius: BorderRadius.circular(25.w)),
                alignment: Alignment.center,
              )
                  .setOnClickListener(() {
                checkInput();
              }, isInkWell: true),
            ],
          ),
          SizedBox(height: 20.w),
        ],
      ).setContainer(padding: EdgeInsets.symmetric(horizontal: 34.w)),
    );
  }

  Future<void> checkInput() async {
    if (companyController.text.isEmpty) {
      ToastUtil.showToast("請輸入聯絡人/公司名稱");
      return;
    }

    if (emailController.text.isEmpty) {
      ToastUtil.showToast("請輸入電郵");
      return;
    }

    if (emailController.text.isNotEmpty &&
        !StringUtil.isEmail(emailController.text)) {
      ToastUtil.showToast("請輸入正確電郵格式");
      return;
    }
    if (themeController.text.isEmpty) {
      ToastUtil.showToast("請輸入主題");
      return;
    }
    if (contentController.text.isEmpty) {
      ToastUtil.showToast("請輸入内容");
      return;
    }
    // var memberInfo = SpUtil.getObject2(AppConstant.memberInfo) as MemberInfo;
    MemberInfo? memberInfo;
    Object? json = SpUtil.getObject2(AppConstant.memberInfo);
    if (json != null) {
      memberInfo = MemberInfo.fromJson2(json);
    }
    Obj.showLoading();
    final params = {
      "contactName": companyController.text,
      "membersEmail": emailController.text,
      "theme": themeController.text,
      "message": contentController.text,
      // "version": _packageInfo?.version ?? "0",
      // "version": "0",
      "membersId": memberInfo?.data?.membersId ?? "",
      "membersName": memberInfo?.data?.membersName ?? "",
      "membersNo": memberInfo?.data?.membersNo ?? "",
    };
    Apis.contact(params).then((value) {
      Obj.dismissLoading();
      var result = ResponseBean.fromJson(value?.data ?? {});
      LogUtil.d("contact success $value");
      if (result.isRequestSuccess) {
        FocusManager.instance.primaryFocus?.unfocus();
        showDialog();
      }
    }).catchError((onError) {
      Obj.dismissLoading();
      LogUtil.d("contact error $onError");
    });
  }

  void showDialog() {
    Get.dialog(
        SimpleDialog(
          titlePadding: EdgeInsets.zero,
          contentPadding: EdgeInsets.zero,
          insetPadding: EdgeInsets.symmetric(horizontal: 30.w),
          backgroundColor: Colors.transparent,
          children: [
            MyConfirmDialogWidget("提交成功", "我們會專人跟進", "回首页", () {
              Get.back();
              Get.back();
            },
                titleTextStyle: getTextStyle(30.sp,
                    color: widget.type == 1
                        ? MyColors.green_69157608
                        : MyColors.green_69157154,
                    fontType: FontType.BOLD),
                subTextStyle: getTextStyle(29.sp,
                    color: MyColors.black_0000011, fontType: FontType.BOLD),
                sureTitleTextStyle: getTextStyle(30.sp,
                    color: Colors.white, fontType: FontType.BOLD),
                sureBackgroundColor: widget.type == 1
                    ? MyColors.green_69157608
                    : MyColors.green_69157154)
          ],
        ),
        barrierDismissible: false,
        barrierColor: const Color.fromRGBO(0, 0, 0, 0.75));
  }
}
